/*
*    l3qd - Light, light, lightweight queue
*    Copyright (C) 2024  Marcus Pedersén marcus@marcux.org
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package list

import (
	"encoding/json"
	"fmt"
	"github.com/spf13/cobra"
	"notabug.org/marcux/l3q/internal/client"
	"notabug.org/marcux/l3q/internal/common"
	"os"
	"strconv"
	"strings"
)

var listNodeCmdFlagVerbose, listNodeCmdFlagHuman bool

var listNodeCmd = &cobra.Command{
	Use:   "node",
	Short: "List information about all nodes",
	Long: `Description:
  List information about all nodes available in l3q.
  To get printing with scaled values use flag --human.`,
	Run: func(cmd *cobra.Command, args []string) {
		printNode()
	},
}

func printNode() {
	nodes := []common.Node{}
	urlSuffix := "/node"

	jsonBytes := client.UrlGet(urlSuffix, listNodeCmdFlagVerbose)
	err := json.Unmarshal(jsonBytes, &nodes)
	if err != nil {
		if listNodeCmdFlagVerbose {
			fmt.Fprintf(os.Stderr, "Failed to decode json string to struct\n%s\n.", err)
		} else {
			fmt.Fprintf(os.Stderr, "Failed to decode json string.\n")
		}
		os.Exit(1)
	}

	headline := strings.Repeat("=", 148)
	line := strings.Repeat("-", 148)
	fmt.Println(headline)
	fmt.Printf("| %s |      %s       |    %s    | %s | %s  |     %s      |   %s   |     %s     |  %s   |        %s        |\n",
		"Nodeid", "Node name", "Cores", "Cores alloc", "Cores used",
		"RAM", "RAM used", "Swap", "Swap used", "State")
	fmt.Println(headline)

	for _, n := range nodes {
		ram := ""
		ramUsed := ""
		swap := ""
		swapUsed := ""
		name := n.Name

		if listNodeCmdFlagHuman {
			ram = client.ScaleHuman(n.MemoryTotal)
			ramUsed = client.ScaleHuman(n.MemoryUsed)
			swap = client.ScaleHuman(n.SwapTotal)
			swapUsed = client.ScaleHuman(n.SwapUsed)
		} else {
			ram = strconv.FormatUint(n.MemoryTotal, 10)
			ramUsed = strconv.FormatUint(n.MemoryUsed, 10)
			swap = strconv.FormatUint(n.SwapTotal, 10)
			swapUsed = strconv.FormatUint(n.SwapUsed, 10)
		}

		if len(name) == 0 {
			if len(n.Hostname) > 20 {
				name = n.Hostname[:20]
			} else {
				name = n.Hostname
			}
		}

		fmt.Printf("| %6d | %-20s | %11d | %11d | %11d | %12s | %12s | %12s | %12s | %-19s |\n",
			n.Id, name, n.CpusTotal, n.CpusAlloc, n.CpusUsed,
			ram, ramUsed, swap, swapUsed, n.State)

		fmt.Println(line)
	}
}

func init() {
	listNodeCmd.Flags().BoolVarP(&listNodeCmdFlagHuman, "human", "H", false, "Printing values in human readable formats")
	listNodeCmd.Flags().BoolVarP(&listNodeCmdFlagVerbose, "verbose", "v", false, "Print verbose output")
}
